import datetime
import requests
import json

def login_session():
    '''
    客流系统权限session
    '''
    url = "http://218.3.159.249:8050/report/users/login"

    payload = "{\"loginName\":\"admin\",\"password\":\"Xck@900950\"}"
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'Content-Type': 'application/json;charset=UTF-8'
    }
    session = requests.session()
    req = session.request("POST", url, headers=headers, data=payload)
    req.content.decode()
    req = json.loads(req.text)
    req = req['data']['atoken']
    return req


def weekly():
    '''
    获取上周同时段客流数据
    :return:
    '''
    now = str(datetime.datetime.now().strftime('%H')) + ":00"
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/yoyOrMom/day/mall?_t=1618108646&orgIds=2&startDate=' + str(
        today) + '&endDate=' + str(today) + '&chartIds=243,244,249,328,329,330,331,332,333,&kpiType=TRAFFIC'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['DayAddYoY']['series']
    insum = []
    point = []
    for i in klreq:
        if i['data'][0] == now:
            insum.append(i['data'][4])
            point.append(i['data'][5])
    return insum[0], point[0]
